/*
 * configMessageFrame.java
 *
 * Created on May 28, 2006, 12:33 AM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package portfolio.configMessage;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.LayoutManager;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.URL;
import java.util.Vector;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JInternalFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;
import portfolio.*;
import misellanea.Miscellanea;

/**
 *
 * @author ACA
 */
public class configMessageFrame extends JInternalFrame implements ActionListener
{
    private final String ACTION_OK     = "vkOkSelected";
    private final String ACTION_CANCEL = "vkCancelSelected";
    
    private final String BUTTON_OK     = "OK";
    private final String BUTTON_CANCEL = "Cancel";
    
    private final String MSG_NOSQLSTATEMENT = "No sql statement provided.";
    
    private Vector     configMessageListeners = new Vector ();
    
    private mainScreen mainFrame     = null;
    private JPanel     buttonArea    = null;
    private JTextField jtName        = null;
    private String     sSQLStatement = null;
    private String     sFieldName    = null;
    private int        iXWidth       = 0;
    private int        iYHeight      = 0;
    private JTextField [] jSqlCreateParams = null;
    /**
     *
     * @param e 
     */
    public void actionPerformed(ActionEvent e) {
        String sAction = e.getActionCommand();
        if        (sAction.equals(this.ACTION_OK)) {
            this.fireConfigMessegeOKSelected ();
            this.dispose();
        } else if (sAction.equals(this.ACTION_CANCEL)) {
            this.fireConfigMessegeCancellSelected ();
            this.dispose();
        }
    } /* end actionPerformed listener */
    
    /** Creates a new instance of configMessageFrame 
     * @param mainFrameParam 
     * @param sSQLStatementParam 
     * @param iXWParam 
     * @param iYHParam 
     * @throws java.lang.Exception 
     */
    public configMessageFrame (mainScreen mainFrameParam, 
                               String     sSQLStatementParam,
                               int        iXWParam,
                               int        iYHParam) throws Exception 
    {
        super (sSQLStatementParam, false, false, false, false);
        sSQLStatement = sSQLStatementParam;
        if (sSQLStatement == null)
            throw new Exception (this.MSG_NOSQLSTATEMENT); //"No sql statement provided.");

        mainFrame = mainFrameParam;
        iXWidth   = iXWParam;
        iYHeight  = iYHParam;
        
        this.setIconLogo();
        this.getContentPane ().setLayout (new BorderLayout ());
        mainFrame.getLayeredPane (). add (this);

        buttonArea = this.setButtonAreaPanel();
        JButton okButton     = this.setNewButton (this.BUTTON_OK, this.ACTION_OK);
        JButton cancelButton = this.setNewButton (this.BUTTON_CANCEL, this.ACTION_CANCEL);

        JPanel nameArea = this.setNameAreaPanel(new FlowLayout ());

        JLabel dbNameLabel = new JLabel ("Name: ");
        nameArea.add (dbNameLabel);
        jtName = new JTextField ( 12);
        nameArea.add(jtName);
    } /* end constructor configMessageFrame */
    public configMessageFrame (mainScreen mainFrameParam,
                               String     sDBName,
                               String     sSQLStatementParam,
                               int        iXWParam,
                               int        iYHParam) throws Exception 
    {
        super (sSQLStatementParam, false, false, false, false);
        sSQLStatement = sSQLStatementParam;
        if (sSQLStatement == null)
            throw new Exception (this.MSG_NOSQLSTATEMENT); //"No sql statement provided.");

        mainFrame = mainFrameParam;
        iXWidth   = iXWParam;
        iYHeight  = iYHParam;
        
        this.setIconLogo();
        this.getContentPane ().setLayout (new BorderLayout ());
        mainFrame.getLayeredPane (). add (this);

        buttonArea = this.setButtonAreaPanel();
        JButton okButton     = this.setNewButton (this.BUTTON_OK, this.ACTION_OK);
        JButton cancelButton = this.setNewButton (this.BUTTON_CANCEL, this.ACTION_CANCEL);

        JPanel nameArea = this.setNameAreaPanel(new FlowLayout ());

        JLabel dbNameLabel = new JLabel ("Name: "+ sDBName);
        nameArea.add (dbNameLabel);
    } /* end constructor configMessageFrame */
    public configMessageFrame (mainScreen mainFrameParam,
                               String     sNounParam,
                               Vector     vSQLStatementParams,
                               int        iXWParam,
                               int        iYHParam) throws Exception 
    {
        super (vSQLStatementParams.get(0).toString(), false, false, false, false);
        sSQLStatement = vSQLStatementParams.get(0).toString();
        if (sSQLStatement == null)
            throw new Exception (this.MSG_NOSQLSTATEMENT); //"No sql statement provided.");

        mainFrame = mainFrameParam;
        iXWidth   = iXWParam;
        iYHeight  = iYHParam;
        
        this.setIconLogo();
        this.getContentPane ().setLayout (new BorderLayout ());
        mainFrame.getLayeredPane (). add (this);

        buttonArea = this.setButtonAreaPanel();
        JButton okButton     = this.setNewButton (this.BUTTON_OK, this.ACTION_OK);
        JButton cancelButton = this.setNewButton (this.BUTTON_CANCEL, this.ACTION_CANCEL);

        JPanel nameArea = this.setNameAreaPanel(new GridLayout (0, 2));

        JLabel dummyLabel = new JLabel ("        ");
        nameArea.add (dummyLabel);
        JLabel dbNounLabel = new JLabel (sNounParam);
        nameArea.add (dbNounLabel);
        
        int iStatementSize = vSQLStatementParams.size() -1;
        if (iStatementSize >= 1) {
            JLabel [] jLabelParams = new JLabel [iStatementSize];
            jSqlCreateParams = new JTextField [iStatementSize];
            
            for (int iIdx = 1; iIdx <= iStatementSize; iIdx++) {
                jLabelParams [iIdx -1] = new JLabel (vSQLStatementParams.get(iIdx).toString());
                nameArea.add(jLabelParams [iIdx -1]);
                jSqlCreateParams [iIdx -1] = new JTextField (12);
                nameArea.add (jSqlCreateParams [iIdx -1]);
            }
        }
    } /* end constructor configMessageFrame */    
    public configMessageFrame (mainScreen mainFrameParam,
                               String     sWinNameParam,
                               Vector     vSQLStatementParams,
                               Vector     vSQLDefaultParams,
                               int        iXWParam,
                               int        iYHParam) throws Exception 
    {
        super (sWinNameParam, false, false, false, false);
        sSQLStatement = sWinNameParam;
        if (sSQLStatement == null)
            throw new Exception (this.MSG_NOSQLSTATEMENT); //"No sql statement provided.");

        mainFrame = mainFrameParam;
        iXWidth   = iXWParam;
        iYHeight  = iYHParam;
        
        this.setIconLogo();
        this.getContentPane ().setLayout (new BorderLayout ());
        mainFrame.getLayeredPane (). add (this);

        buttonArea = this.setButtonAreaPanel ();
        JButton okButton     = this.setNewButton (this.BUTTON_OK, this.ACTION_OK);
        JButton cancelButton = this.setNewButton (this.BUTTON_CANCEL, this.ACTION_CANCEL);

        JPanel nameArea = this.setNameAreaPanel(new GridLayout (0, 2));

        int iStatementSize = vSQLStatementParams.size();
        int iDefaultSize   = vSQLDefaultParams.size ();
        if (iStatementSize != iDefaultSize) {
            throw new Exception (this.MSG_NOSQLSTATEMENT); //"No sql statement provide.");
        }
        if (iStatementSize >= 1) {
            JLabel [] jLabelParams = new JLabel [iStatementSize];
            jSqlCreateParams = new JTextField [iStatementSize];
            
            for (int iIdx = 0; iIdx < iStatementSize; iIdx++) {
                String sLabel = vSQLStatementParams.get(iIdx).toString();
                jLabelParams [iIdx] = new JLabel (sLabel);
                nameArea.add(jLabelParams [iIdx]);
                String sDefault = vSQLDefaultParams.get (iIdx).toString();
                if (!sLabel.contains("Password")) {
                    jSqlCreateParams [iIdx] = new JTextField (sDefault, 12);
                } else {
                    jSqlCreateParams [iIdx] = new JPasswordField (sDefault, 12);
                }
                nameArea.add (jSqlCreateParams [iIdx]);
            }
        }
    } /* end constructor configMessageFrame */    
    /**
     *
     * @return 
     */
    public String getFieldName ()
    {
        sFieldName = jtName.getText();
        return sFieldName;
    }
    /**
     *
     * @param l 
     */
    public synchronized void addConfigMessageListener (configMessageListener l)
    {
        if (configMessageListeners.contains(l)) {
            return;
        }
        configMessageListeners.addElement(l);
        return;
    }
    /**
     *
     * @param l 
     */
    public synchronized void removeConfigMessageListener (configMessageListener l)
    {
        configMessageListeners.removeElement (l);
    }
    /**
     *
     */
    private void fireConfigMessegeOKSelected ()
    {
        Vector vConfigMessage;
        synchronized (this) {
            vConfigMessage = (Vector) configMessageListeners.clone();
        }
        int iSize = vConfigMessage.size();
        if (iSize == 0)
            return;
        
       configMessageEvent event = new configMessageEvent (this);
       for (int iIdx = 0; iIdx < iSize; ++iIdx) {
           configMessageListener listener = (configMessageListener) vConfigMessage.elementAt(iIdx);
           listener.okSelected (event);
       }
    }
    /**
     *
     */
    private void fireConfigMessegeCancellSelected ()
    {
        Vector vConfigMessage;
        synchronized (this) {
            vConfigMessage = (Vector) configMessageListeners.clone();
        }
        int iSize = vConfigMessage.size();
        if (iSize == 0)
            return;
        
       configMessageEvent event = new configMessageEvent (this);
       for (int iIdx = 0; iIdx < iSize; ++iIdx) {
           configMessageListener listener = (configMessageListener) vConfigMessage.elementAt(iIdx);
           listener.cancelSelected (event);
       }
    }    
    /**
     *
     */
    public void showMessageFrame ()
    {
        Miscellanea.showFrameInMidleScr (this, mainFrame, iXWidth, iYHeight);
    }
    /**
     *
     * @return 
     */
    public String getSQLStatementMnemonic ()
    {
        return sSQLStatement;
    }
    /**
     *
     * @return 
     */
    public Vector getSqlCreateParams ()
    {
        int iSqlParamsSize = jSqlCreateParams.length;
        Vector vSqlCreateParams = null;
        if (iSqlParamsSize >= 1) {
            vSqlCreateParams = new Vector ();            
            for (int iIdx = 0; iIdx < iSqlParamsSize; iIdx++) {
                vSqlCreateParams.add(jSqlCreateParams [iIdx].getText().toString());
            }
        }        
        return vSqlCreateParams;
    }
    /**
     * 
     */
    private void setIconLogo ()
    {    
        URL urlIconLogo = mainFrame.getURLIconLogo();
        
        if (urlIconLogo != null) {
            ImageIcon logoIcon = new ImageIcon (urlIconLogo);
            this.setFrameIcon(logoIcon);
        } else {
            mainFrame.displayMessage("configMessageFrame 335", "can't read icon logo.");
        }        
    }
    /**
     * 
     */
    private JPanel setButtonAreaPanel ()
    {
        JPanel buttonPanel = new JPanel ();
        buttonPanel.setLayout (new FlowLayout (FlowLayout.RIGHT));
        this.getContentPane ().add (buttonPanel, BorderLayout.SOUTH);
        
        return buttonPanel;
    }
    /**
     * 
     */
    private JButton setNewButton (String sName, String sAction)
    {
        JButton newButton = new JButton (sName);
        buttonArea.add (newButton);
        newButton.addActionListener (this);
        newButton.setActionCommand  (sAction);
        
        return newButton;
    }
    /**
     * 
     */
    private JPanel setNameAreaPanel (LayoutManager layoutType)
    {
        JPanel nameAreaPanel = new JPanel ();
        nameAreaPanel.setLayout (layoutType); //new GridLayout (0, 2));
        nameAreaPanel.setBorder(new EmptyBorder (5, 5, 5, 5));
        this.getContentPane (). add (nameAreaPanel, BorderLayout.CENTER);
        
        return nameAreaPanel;
    }
} /* end class configMessageFrame */

